package com.example.zhanghao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhanghao.DTO.WorkmessageDto;
import com.example.zhanghao.Entity.User;
import com.example.zhanghao.Entity.Work;
import com.example.zhanghao.Entity.Workmessage;
import com.example.zhanghao.VO.CommonVo;
import com.example.zhanghao.VO.WorkmessageVo;
import com.example.zhanghao.services.impl.WorkServiceImpl;
import com.example.zhanghao.services.impl.WorkmessageServiceImpl;
import com.example.zhanghao.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;

@RestController //这是返回接口
@RequestMapping("/workMessage")
public class WorkMessageController {

    @Autowired
    private WorkmessageServiceImpl workmessageServiceImpl;

    @Autowired
    private WorkServiceImpl workServiceImpl;

    @PostMapping("/add")
    public CommonVo<WorkmessageVo> add(@Valid @RequestBody WorkmessageDto req){
        CommonVo<WorkmessageVo> resp= new CommonVo<>();
        workmessageServiceImpl.save(CopyUtil.copy(req, Workmessage.class));
        resp.setData(CopyUtil.copy(req, WorkmessageVo.class));

        if(req.getType().equals("answer")){
            LambdaQueryWrapper<Work> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Work::getId, req.getWorkId());

            Work work = workServiceImpl.getOne(queryWrapper);
            if(work.getStatus().equals("0")){
                work.setStatus("1");
                workServiceImpl.updateById(work);
            }
        }

        return resp;
    }

}

